/**
 * Package:com.goldcitynet.server.mananger;
 * $Id: MainManager.java,v 1.1 2009/03/28 08:57:13 yangc Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.server;

import com.afteryuan.server.config.Configuration;
//import com.afteryuan.server.grizzly.filter.MyLogFileter;
//import com.afteryuan.server.grizzly.filter.MyMessageDispatchFilter;
//import com.afteryuan.server.grizzly.filter.MyProtocolParserFilter;
import com.afteryuan.server.mina.BinaryCodecFactory;
import com.afteryuan.server.mina.ProtocolBinaryHandler;
//import com.sun.grizzly.*;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filter.logging.MdcInjectionFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * MainManager,这个类是启动服务器的主要控制类.
 * <p/>
 * This class contain the business logic of the application.
 * <p/>
 * All the caches are kept here.
 * <p><a href="MainManager.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: yangc $</a>
 * @version $Revision: 1.1 $
 */
public class MainThread implements Runnable {

    public static final String MINA_FRAME = "mina-frame";
    public static final String GRIZZLY_FRAME = "grizzly-frame";

    private static String serverFrame = MINA_FRAME;

    private Configuration config;

    public MainThread(String serverKeywords, Configuration config) {
        serverFrame = serverKeywords;
        this.config = config;
        SpringContextLoader.getInstance();
    }


    public void run() {
        if (serverFrame.equals(MINA_FRAME)) initMina();
//        else if (serverFrame.equals(GRIZZLY_FRAME)) initGrizzly();
        else throw new IllegalStateException("frame keywords is illegal." + serverFrame);
    }

    protected void initMina() {
        NioSocketAcceptor acceptor = new NioSocketAcceptor();
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

        MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter();
        chain.addLast("mdc", mdcInjectionFilter);
        chain.addLast("codec", new ProtocolCodecFilter(new BinaryCodecFactory()));
        chain.addLast("logger", new LoggingFilter());

        acceptor.setHandler(new ProtocolBinaryHandler());

        try {
            acceptor.bind(new InetSocketAddress(config.getPort()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    protected void initGrizzly() {
//        Controller controller = new Controller();
//
//        TCPSelectorHandler tcpSelectorHandler = new TCPSelectorHandler();
//        tcpSelectorHandler.setPort(config.getPORT());
//        tcpSelectorHandler.setSelectionKeyHandler(new BaseSelectionKeyHandler());//保持长连接，必须使用BaseSelectionKeyHandler
//        controller.addSelectorHandler(tcpSelectorHandler);
//
//        ProtocolChainInstanceHandler pciHandler =
//                new ProtocolChainInstanceHandler() {
//
//                    final private ProtocolChain protocolChain = new DefaultProtocolChain();
//
//                    public ProtocolChain poll() {
//                        return protocolChain;
//                    }
//
//                    public boolean offer(ProtocolChain instance) {
//                        return true;
//                    }
//                };
//        controller.setProtocolChainInstanceHandler(pciHandler);
//        ProtocolChain protocolChain = pciHandler.poll();
//        protocolChain.addFilter(new MyProtocolParserFilter());
//        protocolChain.addFilter(new MyLogFileter());
//        protocolChain.addFilter(new MyMessageDispatchFilter());
//        ((DefaultProtocolChain) protocolChain).setContinuousExecution(true);
//        try {
//            controller.start();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }

    public static String getServerFrame() {
        return serverFrame;
    }
}
